﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Accord.Wavelets;
using AForge.Imaging.Filters;

namespace IBoLT.HelperClasses
{
    class WMetrics
    {
        private System.Drawing.Bitmap im;
		private ResizeNearestNeighbor filter = new ResizeNearestNeighbor(256, 256);
       

        public System.Drawing.Bitmap Im
		{
			get { return im;}
			set {im = filter.Apply(value);}
		}



        public WMetrics() { }
		public WMetrics(System.Drawing.Bitmap im)
		{
			this.Im = im;
		}

        ~WMetrics()
		{
			im.Dispose();
		}


        public int CWMetric()
		{
			IWavelet HaarWavelet = new Haar(2);
			double[,] imdo = new double[im.Width, im.Height];
			Im2DoubleArray(im,imdo);
			HaarWavelet.Forward(imdo);
			
			return Convert.ToInt32(DoubleArraySum(imdo));
        }


        public void Im2DoubleArray( System.Drawing.Bitmap image, double[,] mas)
		{
			System.Drawing.Imaging.BitmapData bmd = SystemDrawingHelper.LockImage(im);
			int size = SystemDrawingHelper.GetPixelSize(bmd);

			System.Drawing.Color clr;

			for (int i = 0; i < image.Width; ++i)
			{
				for (int j = 0; j < image.Height; ++j)
				{
					clr = SystemDrawingHelper.GetPixel(bmd, i, j, size);

					mas[i, j] = Math.Round(Convert.ToDouble(clr.R) * 0.2125 + Convert.ToDouble(clr.G) * 0.7154 + Convert.ToDouble(clr.B) * 0.0721);
				}
			}

			SystemDrawingHelper.UnlockImage(im, bmd);
		}

         public Double DoubleArraySum(double[,] mas)
         {
			double sum=0;
			int l = mas.GetLength(0);
			for (int i = l/2; i < l; ++i)
			{
				for (int j = l / 2; j < l; ++j)
				{
					sum += Math.Abs(mas[i, j]);
				}
			}
			return sum;
			}
        /////////////

    }
}
